chapter:=$(shell pwd |sed 's/.*\///')

.PHONY: main slides exercises problems debug git clean

main: main.tex
	export TEXINPUTS=${TEXINPUTS}:../inputs; xelatex --shell-escape main
	cat $(shell ls ../*/main.aux | sed "/${chapter}/d") >> main.aux
	@if [ -e "main.bcf" ]; then biber main;fi
	export TEXINPUTS=${TEXINPUTS}:../inputs; xelatex --shell-escape main

all:
	for dir in ch*; do \
		cd $${dir}; make main slides exercises summary git; cd ..; \
	done

slides: main.tex
	cp main.tex main_slides.tex
	sed -i '1s/handout//' main_slides.tex
	export TEXINPUTS=${TEXINPUTS}:../inputs; xelatex --shell-escape main_slides

exercises: 
	export TEXINPUTS=${TEXINPUTS}:../inputs; xelatex --shell-escape ../inputs/exercises
	cat $(shell ls ../*/exercises.aux | sed "/${chapter}/d") >> exercises.aux
	@if [ -e "exercises.bcf" ]; then biber exercises;fi
	export TEXINPUTS=${TEXINPUTS}:../inputs; xelatex --shell-escape ../inputs/exercises.tex

exercises_book: exercises.tex
	export TEXINPUTS=${TEXINPUTS}:../inputs; xelatex --shell-escape exercises
	biber exercises
	export TEXINPUTS=${TEXINPUTS}:../inputs; xelatex --shell-escape exercises

debug:
	# f = file 表示要调试的单个文件
	cp ${f}.tex .debug.tex
	export TEXINPUTS=${TEXINPUTS}:../inputs; xelatex --shell-escape ../inputs/debug.tex

summary:
	../inputs/summary
	xelatex summary.tex

comments: 
	xelatex comments

git:
	@if [ -e main.pdf ]; then cp -u main.pdf ../pdfs/${chapter}.pdf; fi
	@if [ -e main_slides.pdf ]; then cp -u main_slides.pdf ../pdfs/${chapter}_slides.pdf; fi
	@if [ -e exercises.pdf ]; then cp -u exercises.pdf ../pdfs/${chapter}_exercises.pdf; fi
	@if [ -e summary.pdf ]; then cp -u summary.pdf ../pdfs/${chapter}_summary.pdf; fi
	@if [ -e exercises_slides.pdf ]; then cp -u exercises_slides.pdf ../pdfs/${chapter}_exercises_slides.pdf; fi
	#@if [ -e problems.pdf ]; then cp -u problems.pdf ../git/${chapter}_problems.pdf; fi
	#cd ../git;./push.sh
	
clean:
	-rm -f -r _minted* *.aux *.bcf *.xml *.out *.toc *.snm *.nav *.log *.vrb *.blg *.bbl *.ptc *.idx debug.pdf temp_*
